package com.jiaai.cloud.wx.controller;

import com.jiaai.cloud.base.entity.RestResultGenerator;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.wx.entity.TWxshopclass;
import com.jiaai.cloud.wx.service.ITWxshopclassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2017-09-08
 */
@Controller
@RequestMapping("/api/v1/twxshopclass")
public class TWxshopclassController {

    @Autowired
    private ITWxshopclassService itWxshopclassService;

    //todo
    /**
     * 首页获取所有商店分类
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/getall",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object getAll() throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itWxshopclassService.selectList(null));
        } catch (Exception e){
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    //todo
    /**
     * 获取单个商店分类信息
     * @param id
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/getone/{id}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object getOne(@PathVariable("id") Integer id) throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itWxshopclassService.selectById(id));
        } catch (Exception e){
            throw new ServiceException("查询失败", e.getMessage());
        }
    }


    //todo
    /**
     * 添加商店分类
     * @param tWxshopclass
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object add(@RequestBody TWxshopclass tWxshopclass) throws ServiceException {
        itWxshopclassService.insert(tWxshopclass);
        try {
            return RestResultGenerator.genSuccessResult("添加成功");
        } catch (Exception e){
            throw new ServiceException("添加失败", e.getMessage());
        }
    }

    //todo
    /**
     * 删除商店分类
     * @param id
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object delete(@PathVariable("id") Integer id) throws ServiceException {
        itWxshopclassService.deleteById(id);
        try {
            return RestResultGenerator.genSuccessResult("删除成功");
        } catch (Exception e){
            throw new ServiceException("删除失败", e.getMessage());
        }
    }

    //todo
    /**
     * 编辑商店分类信息
     * @param tWxshopclass
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object update(@RequestBody TWxshopclass tWxshopclass) throws ServiceException {
        itWxshopclassService.updateById(tWxshopclass);
        try {
            return RestResultGenerator.genSuccessResult("编辑成功");
        } catch (Exception e){
            throw new ServiceException("编辑失败", e.getMessage());
        }
    }
}
